import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 设置动态路由
export const setRoutes = () => {
  const storeMenu = localStorage.getItem('menus')
  if(storeMenu){
    // 拼接动态路由
    const manageRoute = {
      path: '/',
      name: 'Manage',
      component: () => import('../views/Manage.vue'),
      children: [
      
      ]
    }

    const menus = JSON.parse(storeMenu)
    menus.forEach(item => {
      if(item.path){
        let itemMenu = {
          path: item.path.replace('/', ''),
          name: item.name,
          component: () => import(`../views/${item.pagePath}.vue`)
        }
        manageRoute.children.push(itemMenu)
      }else if(item.children.lenth){
        item.children.forEach(subItem => {
          if(subItem.path){
            let itemMenu = {
              path: subItem.path.replace('/', ''),
              name: subItem.name,
              component: () => import(`../views/${subItem.pagePath}.vue`)
            }
            manageRoute.children.push(itemMenu)
          }
        })
      }
    })

    // 动态添加路由
    router.addRoute(manageRoute)

  }
}


export default router
